// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: SS Results Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0  : object.timer
private alias object.value1  : object.iconSheet
private alias object.value2  : object.textSheet
private alias object.value3  : object.round1Winner
private alias object.value4  : object.round2Winner
private alias object.value5  : object.round3Winner
private alias object.value6  : object.round1Results
private alias object.value7  : object.round2Results
private alias object.value8  : object.round3Results
private alias object.value9  : object.wins1P
private alias object.value10 : object.wins2P
private alias object.value11 : object.overallWinner
private alias object.value12 : object.vsReady
private alias object.value13 : object.hasTieBreaker

// States
private alias 0 : SSRESULTS_SETUP
private alias 1 : SSRESULTS_WAITING
private alias 2 : SSRESULTS_SHOWRESULTS
private alias 3 : SSRESULTS_FADEOUT
private alias 4 : SSRESULTS_RETURNTOZONESEL

// 2PVSSetup Aliases
private alias object.value0 : 2PVSSetup.timer

// 2PVSSetup States
private alias 1 : 2PVSSETUP_FADEIN


// ========================
// Events
// ========================

event ObjectUpdate
	switch object.state
	case SSRESULTS_SETUP
		SetupMenu(MENU_1, 0, 3, 0)

		LoadSpriteSheet("LevelSelect/Icons.gif")
		object.iconSheet = object.spriteSheet

		LoadSpriteSheet("LevelSelect/Text.gif")
		object.textSheet = object.spriteSheet

		menu1.selection = -1
		arrayPos0 = vs.match

		arrayPos0 <<= 2
		arrayPos0 += 0x400
		object.round1Results = saveRAM[arrayPos0]

		arrayPos0++
		object.round2Results = saveRAM[arrayPos0]

		arrayPos0++
		object.round3Results = saveRAM[arrayPos0]

		object.wins1P = 0
		object.wins2P = 0

		temp0 = object.round1Results
		temp0 &= 15
		temp1 = object.round1Results
		temp1 >>= 4
		if temp0 == temp1
			object.round1Winner = 3
		else
			if temp0 > temp1
				object.round1Winner = 1
				object.wins1P++
			else
				object.round1Winner = 2
				object.wins2P++
			end if
		end if

		temp0 = object.round2Results
		temp0 &= 15
		temp1 = object.round2Results
		temp1 >>= 4
		if temp0 == temp1
			object.round2Winner = 3
		else
			if temp0 > temp1
				object.round2Winner = 1
				object.wins1P++
			else
				object.round2Winner = 2
				object.wins2P++
			end if
		end if
		object.hasTieBreaker = vs.tieBreaker
		vs.tieBreaker = false

		temp0 = object.round3Results
		temp0 &= 15
		temp1 = object.round3Results
		temp1 >>= 4
		if temp0 == temp1
			object.round3Winner = 3
		else
			if temp0 > temp1
				object.round3Winner = 1
				object.wins1P++
			else
				object.round3Winner = 2
				object.wins2P++
			end if
		end if

		if object.wins1P == object.wins2P
			object.overallWinner = 18
		else
			if object.wins1P > object.wins2P
				object.overallWinner = 14
			else
				object.overallWinner = 16
			end if
		end if

		AddMenuEntry(MENU_1, "@@@@@@@@@CLEQYPOBEHZ", true)
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "@@@@@@@@@@@@@@@1N@@@@@2N@@@@LOGEDNV", true)
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "YPOBEHZ@@* 1 *@@@@@@@:", true)
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "YPOBEHZ@@* 2 *@@@@@@@:", true)
		AddMenuEntry(MENU_1, "@", true)
		if object.hasTieBreaker == false
			AddMenuEntry(MENU_1, "YPOBEHZ@@* 3 *@@@@HE NSPAVN", true)
		else
			AddMenuEntry(MENU_1, "YPOBEHZ@@* 3 *@@@@@@@:", true)
		end if

		object.state++
		break

	case SSRESULTS_WAITING
		CallNativeFunction2(ReceiveEntity, 32, true)
		ResetObjectEntity(32, TypeName[Blank Object], 0, 0, 0)
		object.state++
		break

	case SSRESULTS_SHOWRESULTS
		if object.timer < 60
			object.timer++
		else
			CheckTouchRect(0, 0, screen.xsize, screen.ysize)
			if keyPress[0].start == true
				checkResult = 0
			end if

			if keyPress[0].buttonA == true
				checkResult = 0
			end if

			if keyPress[0].buttonB == true
				checkResult = 0
			end if

			if keyPress[0].buttonC == true
				checkResult = 0
			end if

			if checkResult > -1
				object.timer = 0
				vs.match++
				object.state = SSRESULTS_FADEOUT
				CallNativeFunction2(SendValue, 1, true)
				object.vsReady = false
			end if
		end if
		break

	case SSRESULTS_FADEOUT
		if object.timer < 384
			object.timer += 16
			if vs.match < vs.gameLength
				music.volume -= 10
			end if
			SetScreenFade(0x00, 0x00, 0x00, object.timer)
		else
			if vs.match >= vs.gameLength
				ResetObjectEntity(1, TypeName[Game Results], 0, 0, 0)
				object[1].priority = PRIORITY_ACTIVE
				object[0].state = 2PVSSETUP_FADEIN
				2PVSSetup[0].timer = 384
			else
				object.state = SSRESULTS_RETURNTOZONESEL
			end if
			SetScreenFade(0x00, 0x00, 0x00, 0xFF)
		end if
		break

	case SSRESULTS_RETURNTOZONESEL
		if ReceiveValue == -1
			// If the ReceiveValue function doesn't even exist, then force the object to be ready
			object.vsReady = true
		else
			CallNativeFunction2(ReceiveValue, object.vsReady, true)
		end if

		if object.vsReady == true
			SetMusicTrack("Options.ogg", 0, true)
			PlayMusic(0)

			ResetObjectEntity(1, TypeName[2P VS Menu 3], 0, 0, 0)
			object[1].priority = PRIORITY_ACTIVE
			object[0].state = 2PVSSETUP_FADEIN
			2PVSSetup[0].timer = 384
			SetScreenFade(0x00, 0x00, 0x00, 0xFF)
		end if
		break
		
	end switch

	object.animationTimer++
	object.animationTimer &= 15
end event


event ObjectDraw
	object.spriteSheet = object.iconSheet
	if object.state < SSRESULTS_RETURNTOZONESEL
		// RESULT
		DrawSpriteScreenXY(0, screen.xcenter, 17)
		temp0 = screen.xcenter
		temp1 = temp0
		temp1 += 48

		// Round 1
		temp2 = object.round1Results
		temp2 &= 15
		temp3 = object.round1Results
		temp3 >>= 4
		DrawNumbers(4, temp0, 129, temp2, 2, 8, false)
		DrawNumbers(4, temp1, 129, temp3, 2, 8, false)

		// Round 2
		temp2 = object.round2Results
		temp2 &= 15
		temp3 = object.round2Results
		temp3 >>= 4
		DrawNumbers(4, temp0, 145, temp2, 2, 8, false)
		DrawNumbers(4, temp1, 145, temp3, 2, 8, false)

		// Round 3
		if object.hasTieBreaker == true
			temp2 = object.round3Results
			temp2 &= 15
			temp3 = object.round3Results
			temp3 >>= 4
			DrawNumbers(4, temp0, 161, temp2, 2, 8, false)
			DrawNumbers(4, temp1, 161, temp3, 2, 8, false)
		end if

		// Round Winners
		temp0 = screen.xcenter
		temp0 += 112
		DrawSpriteScreenXY(object.round1Winner, temp0, 129)
		DrawSpriteScreenXY(object.round2Winner, temp0, 145)
		if object.hasTieBreaker == true
			DrawSpriteScreenXY(object.round3Winner, temp0, 161)
		end if
		temp0 = object.animationTimer
		temp0 >>= 3
		temp0 += object.overallWinner
		DrawSpriteScreenXY(temp0, screen.xcenter, 196)

		// Menu
		object.spriteSheet = object.textSheet
		temp0 = screen.xcenter
		temp0 -= 128
		DrawMenu(MENU_1, temp0, 64)
	else
		// Waiting...
		DrawRect(0, 0, screen.xsize, screen.ysize, 0, 0, 0, 255)
		DrawSpriteScreenXY(20, screen.xsize, screen.ysize)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("LevelSelect/Icons.gif")
	LoadSpriteSheet("LevelSelect/Text.gif")

	SpriteFrame(-48, 0, 96, 14, 1, 136)		// RESULT				- #0
	SpriteFrame(-8, 0, 16, 7, 98, 136)		// 1P text 				- #1
	SpriteFrame(-8, 0, 16, 7, 115, 136)		// 2P text 				- #2
	SpriteFrame(-16, 0, 32, 7, 132, 136) 	// TIED text			- #3
	SpriteFrame(0, 0, 8, 7, 175, 140)		// 0 					- #4
	SpriteFrame(0, 0, 8, 7, 183, 140)		// 1 					- #5
	SpriteFrame(0, 0, 8, 7, 191, 140)		// 2 					- #6
	SpriteFrame(0, 0, 8, 7, 199, 140)		// 3 					- #7
	SpriteFrame(0, 0, 8, 7, 207, 140)		// 4 					- #8
	SpriteFrame(0, 0, 8, 7, 215, 140)		// 5 					- #9
	SpriteFrame(0, 0, 8, 7, 223, 140)		// 6 					- #10
	SpriteFrame(0, 0, 8, 7, 231, 140)		// 7 					- #11
	SpriteFrame(0, 0, 8, 7, 239, 140)		// 8 					- #12
	SpriteFrame(0, 0, 8, 7, 247, 140)		// 9 					- #13
	SpriteFrame(-47, 0, 91, 12, 1, 110)		// 1P WINS 				- #14
	SpriteFrame(-47, 0, 91, 12, 1, 123)		// 1P WINS (Flashing) 	- #15
	SpriteFrame(-47, 0, 91, 12, 93, 110)	// 2P WINS 				- #16
	SpriteFrame(-47, 0, 91, 12, 93, 123)	// 2P WINS (Flashing) 	- #17
	SpriteFrame(-22, 0, 44, 12, 1, 385)		// TIED 				- #18
	SpriteFrame(-22, 0, 44, 12, 1, 398)		// TIED (Flashing) 		- #19
	if vs.playerID == 0
		SpriteFrame(-160, -16, 136, 7, 103, 422)	// WAITING FOR 2P... 	- #20
	else
		SpriteFrame(-160, -16, 136, 7, 103, 414)	// WAITING FOR 1P... 	- #20
	end if

	foreach (TypeName[SS Results], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_ACTIVE
	next
end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("LevelSelect/Icons.gif")
	SpriteFrame(-48, 0, 96, 14, 1, 136)		// RESULT				- #0
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
